package com.sidianzhong.sdz.controller;

import com.alibaba.fastjson.JSONObject;
import com.sidianzhong.sdz.annotation.BackLoginToken;
import com.sidianzhong.sdz.annotation.UserLoginToken;
import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.service.commond.RedisService;
import com.sidianzhong.sdz.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import java.util.stream.Collectors;

/**
 * Created by hxg on 2016/10/06.
 */
@Api(description = "我的团队")
@Controller
public class MyTeamController {

    @Autowired
    MyTeamService myTeamService;
    @Autowired
    HttpServletRequest request;
    @Autowired
    HttpServletResponse response;

    @BackLoginToken
    @ApiOperation(value = "创建'我的团队'表中一条信息")
    @RequestMapping(value = "/myTeam/new", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object createMyTeam(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "investId", required = false) Integer investId,
            @RequestParam(value = "level", required = false) Integer level,
            @RequestParam(value = "userId", required = false) Integer userId,
            @RequestParam(value = "limits", required = false) Double limits,
            @RequestParam(value = "createTime", required = false) String createTime,
            @RequestParam(value = "lsatUpdateTime", required = false) String lsatUpdateTime
    ) {
        MyTeam item = new MyTeam();
        Date date = new Date();
        if (investId != null) {
            item.setInvestId(investId);
        }
        if (level != null) {
            item.setLevel(level);
        }
        if (userId != null) {
            item.setUserId(userId);
        }
        if (limits != null) {
            item.setLimits(limits);
        }
        item.setCreateTime(new Date());
        if (lsatUpdateTime != null) {
            item.setLsatUpdateTime(date);
        }
        MyTeam iteams = myTeamService.create(item);
        boolean result = iteams != null;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        return new ResponseEntity<>(ResultModel.ok(jsonObject), HttpStatus.OK);
    }

    @BackLoginToken
    @ApiOperation(value = "删除'我的团队'表中的某条记录")
    @RequestMapping(value = "/myTeam/delete", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object deleteMyTeam(
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "id") Integer id
    ) {
        MyTeam item = myTeamService.get(id);
        if (null == item) {
            return new ResponseEntity<>(ResultModel.error(), HttpStatus.OK);
        }
        int delete = myTeamService.delete(id);
        boolean result = delete != 0;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        return new ResponseEntity<>(ResultModel.ok(jsonObject), HttpStatus.OK);
    }

    @BackLoginToken
    @ApiOperation(value = "修改'我的团队'表中的某条记录")
    @RequestMapping(value = "/myTeam/edit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object editMyTeam(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "id") Integer id,
            @RequestParam(value = "investId", required = false) Integer investId,
            @RequestParam(value = "level", required = false) Integer level,
            @RequestParam(value = "userId", required = false) Integer userId,
            @RequestParam(value = "limits", required = false) Double limits,
            @RequestParam(value = "lsatUpdateTime", required = false) String lsatUpdateTime
    ) {
        MyTeam item = myTeamService.get(id);
        if (null == item) {
            return new ResponseEntity<>(ResultModel.error(), HttpStatus.OK);
        }
        if (investId != null) {
            item.setInvestId(investId);
        }
        if (level != null) {
            item.setLevel(level);
        }
        if (userId != null) {
            item.setUserId(userId);
        }
        if (limits != null) {
            item.setLimits(limits);
        }
        if (lsatUpdateTime != null) {
            item.setLsatUpdateTime(Tools.convertStringToDate(lsatUpdateTime, "yyyy-MM-dd HH:mm:ss"));
        }
        int update = myTeamService.update(item);
        boolean result = update != 0;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        return new ResponseEntity<>(ResultModel.ok(jsonObject), HttpStatus.OK);
    }

    @UserLoginToken
    @ApiOperation(value = "查询'我的团队'表中的某条记录")
    @RequestMapping(value = "/getMyTeamById", method = {RequestMethod.GET}, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getMyTeamById(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "id") Integer id
    ) {
        MyTeam item = myTeamService.get(id);
        if (null == item) {
            return new ResponseEntity<>(ResultModel.error(), HttpStatus.OK);
        }
        return new ResponseEntity<>(ResultModel.ok(item), HttpStatus.OK);
    }

    @UserLoginToken
    @ApiOperation(value = "查询'我的团队'表中的多条记录返回对象")
    @RequestMapping(value = "/myTeams", method = {RequestMethod.GET}, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getMyTeams(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token", required = false) String token,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortItem", required = false, defaultValue = "id") String sortItem,
            @RequestParam(value = "sortOrder", required = false, defaultValue = "desc") String sortOrder,
            @RequestParam(value = "investId", required = false) Integer investId,
            @RequestParam(value = "level", required = false) Integer level,
            @RequestParam(value = "userId", required = false) Integer userId,
            @RequestParam(value = "limits", required = false) Double limits,
            @RequestParam(value = "createTime", required = false) String createTime,
            @RequestParam(value = "lsatUpdateTime", required = false) String lsatUpdateTime
    ) {
        MyTeam item = new MyTeam();
        if (investId != null) {
            item.setInvestId(investId);
        }
        if (level != null) {
            item.setLevel(level);
        }
        if (userId != null) {
            item.setUserId(userId);
        }
        if (limits != null) {
            item.setLimits(limits);
        }
        if (createTime != null) {
            item.setCreateTime(Tools.convertStringToDate(createTime, "yyyy-MM-dd HH:mm:ss"));
        }
        if (lsatUpdateTime != null) {
            item.setLsatUpdateTime(Tools.convertStringToDate(lsatUpdateTime, "yyyy-MM-dd HH:mm:ss"));
        }
        PageInfo<MyTeam> list = myTeamService.getListWithPaging(pageNum, pageSize, sortItem, sortOrder, item);
        return new ResponseEntity<>(ResultModel.ok(list), HttpStatus.OK);
    }

    @UserLoginToken
    @ApiOperation(value = "查询'我的团队'表中的多条记录返回自定义JSONObject")
    @RequestMapping(value = "/myTeamsObject", method = {RequestMethod.GET}, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getMyTeamsObject(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token", required = false) String token,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortItem", required = false, defaultValue = "id") String sortItem,
            @RequestParam(value = "sortOrder", required = false, defaultValue = "desc") String sortOrder,
            @RequestParam(value = "investId", required = false) Integer investId,
            @RequestParam(value = "level", required = false) Integer level,
            @RequestParam(value = "userId", required = false) Integer userId,
            @RequestParam(value = "limits", required = false) Double limits,
            @RequestParam(value = "createTime", required = false) String createTime,
            @RequestParam(value = "lsatUpdateTime", required = false) String lsatUpdateTime
    ) {
        MyTeam item = new MyTeam();
        if (investId != null) {
            item.setInvestId(investId);
        }
        if (level != null) {
            item.setLevel(level);
        }
        if (userId != null) {
            item.setUserId(userId);
        }
        if (limits != null) {
            item.setLimits(limits);
        }
        if (createTime != null) {
            item.setCreateTime(Tools.convertStringToDate(createTime, "yyyy-MM-dd HH:mm:ss"));
        }
        if (lsatUpdateTime != null) {
            item.setLsatUpdateTime(Tools.convertStringToDate(lsatUpdateTime, "yyyy-MM-dd HH:mm:ss"));
        }
        PageInfo<JSONObject> list = myTeamService.getListWithObject(pageNum, pageSize, sortItem, sortOrder, item);
        List<JSONObject> list1 = list.getList();
        List<JSONObject> collect = list1.stream().map(ls -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", ls.getInteger("id"));
            jsonObject.put("investId", ls.getInteger("investId"));
            jsonObject.put("level", ls.getInteger("level"));
            jsonObject.put("userId", ls.getInteger("userId"));
            jsonObject.put("limits", ls.getDouble("limits"));
            jsonObject.put("createTime", ls.getDate("createTime"));
            jsonObject.put("lsatUpdateTime", ls.getDate("lsatUpdateTime"));
            return jsonObject;
        }).collect(Collectors.toList());
        list.setList(collect);
        return new ResponseEntity<>(ResultModel.ok(list), HttpStatus.OK);
    }
}
